using UnityEngine;
using UnityEngine.UI;

public class CharacterInfoUI : MonoBehaviour
{
    private Player player;
    private PlayerStats stats;

    [SerializeField] private Slider hpSlider = null;
    [SerializeField] private CamberSlider energySlider_1 = null;
    [SerializeField] private CamberSlider energySlider_2 = null;

    [SerializeField] private Image weaponIcon = null;

    private void Start()
    {
        player = PlayerManager.Instance.player;
        stats = player.gameObject.GetComponent<PlayerStats>();

        stats.onHealthChanged += UpdataHPUI;
        stats.onEnergy_1Changed += UpdateEnergy_1UI;
        stats.onEnergy_2Changed += UpdateEnergy_2UI;
        player.onCurrentWeaponChanged += UpdateCurrentWeaponIconUI;
    }

    #region Update UI Function

    private void UpdataHPUI()
    {
        hpSlider.maxValue = stats.GetFinalMaxHP();
        hpSlider.value = stats.currentHP;
    }

    private void UpdateEnergy_1UI()
    {
        if (energySlider_1 == null) return;
        energySlider_1.fillRange = stats.currentEnergy_1 / stats.maxEnergy_1.GetValue() * energySlider_1.maxFill;
    }

    private void UpdateEnergy_2UI()
    {
        if (energySlider_2 == null) return;
        energySlider_2.fillRange = stats.currentEnergy_2 / stats.maxEnergy_2.GetValue() * energySlider_2.maxFill;
    }

    private void UpdateCurrentWeaponIconUI()
    {

        weaponIcon.sprite = player.currentWeapon.Icon;
    }
    #endregion

    private void OnDisable()
    {
        stats.onHealthChanged -= UpdataHPUI;
        stats.onEnergy_1Changed -= UpdateEnergy_1UI;
        stats.onEnergy_2Changed -= UpdateEnergy_2UI;
        player.onCurrentWeaponChanged -= UpdateCurrentWeaponIconUI;
    }
}
